import * as $util from '/lib/hc-util.js'

var refercontrol = {
    props: {
        definition: {
            type: Object,
            default () {
                return {};
            }
        }
    },
    watch: {
        "definition": { //load the reference list when show dialog
            immediate: true,
            handler: function(nv, ov) {
                if (!this.definition.settingList) {
                    this.$set(this.definition, 'settingList', $pinfo.ruleList.filter(it => it.rtype ===
                        'data:encoder'));
                        
                    this.$set(this.definition, 'resultStruct', $config.componentMap['data:encoder'].resultStruct
                        .root);
                    $util.$tree.sort(this.definition.resultStruct.list, (a, b) => b.important - a.important);
                }
            }
        }
    },
    methods: {
        handleChange: function(val) {
            let encoder = $pinfo.ruleList.find(it => it.rpath == this.definition.refer);
            let lst = encoder ? encoder.list : [];
            $util.$tree.traverse(lst, (it) => it.label = it.name ? it.code + ':' + it.name : it.code);
            // console.warn('handleReferChange',val,lst);
            this.$set(this.definition, 'inputList', lst)
        }
    },
    template: `
    <el-form-item label="编码场景" prop='refer' :rules="[{
    				required: true,
    				message: '请选编码设置的业务场景',
    				trigger: 'change'
    			}
    		]">
    
       <el-cascader style='width: 100%;'
            :props="{label:'rname',value:'rpath',children:'null',expandTrigger:'hover',emitPath:false}"
            v-model='definition.refer' placeholder="关键字搜索" :options="definition.settingList"
            filterable @change="handleChange">
        </el-cascader>
    </el-form-item>
   `,
};

var inputcontrol = {
    props: {
        value: {
            type: [Number],
            default: 0
        },
        definition: {
            type: Object,
            default () {
                return {};
            }
        }
    },
    data: function() {
        return {
            inputValue: null,
        }
    },
    created: function() {
        this.inputValue = this.value;
    },
    watch: {
        "definition": {
            immediate: true,
            handler: function(nv, ov) {
                //the rule definition will be reloaded after save, cause to clear the temporary data such as inputList.
                if (!this.definition.inputList) {
                    let encoder = $pinfo.ruleList.find(it => it.rpath == this.definition.refer);
                    let lst = encoder ? encoder.list : [];
                    $util.$tree.traverse(lst, (it) => it.label = it.name ? it.code + ':' + it.name : it.code);
                    this.$set(this.definition, 'inputList', lst)
                }
            },
        },
        "definition.inputList": {
            immediate: true,
            handler: function(nv, ov) {
                console.warn('definition.inputList',nv,ov,this.definition.inputList)
                if (!this.definition.inputList) return;
                if (!$util.$tree.find(this.definition.inputList, this.inputValue)) {
                    this.inputValue = null;
                    this.handleChange(this.inputValue);
                }
            },
        },
    },
    methods: {
        handleChange: function(val) {
            this.$emit('input', val);
        }
    },
    template: `
        <el-cascader style='width: 100%;'
            :props="{label:'label',value:'id',children:'list',expandTrigger:'hover',emitPath:false}"
            v-model='inputValue' placeholder="可搜索" :options="definition.inputList"
            filterable clearable @change="handleChange">
        </el-cascader>
        `
};

var actionconfig = {
    code: 'EncoderSetter',
    name: '业务编码设置',
    refer: refercontrol,
    input: inputcontrol,
};

export default actionconfig;
